<?php

/**
 * Database token
 * Copyright 2009 Matt Basta
 * 
 */

class cloud_token {
	/**
	 * Secure variables:
	 *  table	- Database table
	 *   > A string, contains the table name
	 *  index	- Table index
	 *   > An array, consider a conditional array providing an intersection
	 *  data	- Currently available column data
	 *   > An array, contains cached remote values for the row
	 *  fetched	- Has column data been fetched
	 *   > Boolean, describes whether the data has been downloaded yet
	 */
	
	public function __construct($table, $index) {
		// Securely store the database information
		self::secure( 'table',		$table );
		self::secure( 'index',		$index );
		
		self::secure( 'fetched',	false );
	}
	
	// Basic CRUD functions (excluding creation; token already exists!)
	public function __get($name) {
		if(self::secure('fetched')) {
			$data = self::secure('data');
		} else {
			$driver = self::secure('driver');
		}
		return $data[$name];
	}
	public function __set($name, $value) {
		
		// Get the database info
		$table = self::secure('table');
		$index = self::secure('index');
		
		// Perform the query
		$table->update_row(
			$table,
			array( $name => $value ),
			$index
		);
		
		// Reset the value within the secure storage for caching's sake
		$data = self::secure('data');
		$data[$name] = $value;
		self::secure('data', $data);
	}
	public function destroy() {
		$table = self::secure('table');
		$table->delete_row(self::secure('index'));
		
		self::secure('fetched', false);
		self::secure('table', false, true);
		self::secure('index', false, true);
		
	}
	
}